/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.servlet;

import aptech.container.Constants;
import aptech.data.ActivityBO;
import aptech.data.ScheduleActivityBO;
import aptech.data.TypeOfActivityBO;
import aptech.entities.Activity;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author home
 */
@WebServlet(name = "ActivityServlet", urlPatterns = {"/ActivityServlet"})
public class ActivityServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String action = request.getParameter(Constants.ACTION);
            
            if (action.equals(Constants.ACTION_ADD)) {

                String scheduleid = request.getParameter(Constants.SCHEDULE_ACTIVITY_ID);
                String type_activityid = request.getParameter(Constants.TYPE_ACTIVITY_ID);
                String title = request.getParameter(Constants.ACTIVITY_TITLE);
                String date = request.getParameter(Constants.ACTIVITY_DATE);
                String timeduration = request.getParameter(Constants.ACTIVITY_TIME_DURATION);
                String location = request.getParameter(Constants.ACTIVITY_LOCATION);
                String fee = request.getParameter(Constants.ACTIVITY_FEE);
                String current_people = request.getParameter(Constants.ACTIVITY_CURRENT_PEOPLE);
                String limitted_people = request.getParameter(Constants.ACTIVITY_LIMITTED_PEOPLE);
                String email = request.getParameter(Constants.ACTIVITY_EMAIL_ORGANIZER);
                String description = request.getParameter(Constants.ACTIVITY_DESCRIPTION);
                String status = request.getParameter(Constants.ACTIVITY_STATUS);

                if (scheduleid != null) {
                    if (title == null) {
                        title = "title";
                    }
                    if (type_activityid == null) {
                        type_activityid = "1";
                    }
                    if (date == null) {
                        date = "30/01/2012";
                    }
                    if (timeduration == null) {
                        timeduration = "09:30 AM - 10:30 AM";
                    }
                    if (location == null) {
                        location = "Position X";
                    }
                    if (fee == null) {
                        fee = "0";
                    }
                    if (current_people == null) {
                        current_people = "0";
                    }
                    if (limitted_people == null) {
                        limitted_people = "10";
                    }
                    if (email == null) {
                        email = "adminorganizer@gmail.com";
                    }
                    if (description == null) {
                        description = "description";
                    }
                    if (status == null) {
                        status = "False";
                    }
                    Activity activity = new Activity();
                    activity.setScheduleActivity(new ScheduleActivityBO().getById(Integer.parseInt(scheduleid), true));
                    activity.setTypeOfActivity(new TypeOfActivityBO().getById(Integer.parseInt(type_activityid), true));
                    activity.setTitleActivity(title);
                    activity.setDateOfActivity(new SimpleDateFormat("dd/MM/yyyy").format(new SimpleDateFormat("dd/MM/yyyy").parse(date)));
                    activity.setTimeDuaration(timeduration);
                    activity.setLocaltion(location);
                    activity.setFee(Integer.parseInt(fee));
                    activity.setCurrentPeople(Integer.parseInt(current_people));
                    activity.setLimittedPeople(Integer.parseInt(limitted_people));
                    activity.setEmailOfOrganizer(email);
                    activity.setDescription(description);
                    activity.setStatusOfActivity(status);
                    //add
                    boolean kq = new ActivityBO().addNew(activity);
                    if (kq) {
                        out.println("<result>success</result>");
                    }

                } else {
                    out.println("<result>empty</result>");
                }
            }
            
            if (action.equals(Constants.ACTION_EDIT)) {
                String activityid = request.getParameter(Constants.ACTIVITY_ID);
                Activity activity = new ActivityBO().getById(Integer.parseInt(activityid), true);
                if (activity != null) {
                    String scheduleid = request.getParameter(Constants.SCHEDULE_ACTIVITY_ID);
                    String type_activityid = request.getParameter(Constants.TYPE_ACTIVITY_ID);
                    String title = request.getParameter(Constants.ACTIVITY_TITLE);
                    String date = request.getParameter(Constants.ACTIVITY_DATE);
                    String timeduration = request.getParameter(Constants.ACTIVITY_TIME_DURATION);
                    String location = request.getParameter(Constants.ACTIVITY_LOCATION);
                    String fee = request.getParameter(Constants.ACTIVITY_FEE);
                    String current_people = request.getParameter(Constants.ACTIVITY_CURRENT_PEOPLE);
                    String limitted_people = request.getParameter(Constants.ACTIVITY_LIMITTED_PEOPLE);
                    String email = request.getParameter(Constants.ACTIVITY_EMAIL_ORGANIZER);
                    String description = request.getParameter(Constants.ACTIVITY_DESCRIPTION);
                    String status = request.getParameter(Constants.ACTIVITY_STATUS);
                    if (scheduleid != null) {
                        if (title == null) {
                            title = "title";
                        }
                        if (type_activityid == null) {
                            type_activityid = "1";
                        }
                        if (date == null) {
                            date = "30/01/2012";
                        }
                        if (timeduration == null) {
                            timeduration = "09:30 AM - 10:30 AM";
                        }
                        if (location == null) {
                            location = "Position X";
                        }
                        if (fee == null) {
                            fee = "0";
                        }
                        if (current_people == null) {
                            current_people = "0";
                        }
                        if (limitted_people == null) {
                            limitted_people = "10";
                        }
                        if (email == null) {
                            email = "adminorganizer@gmail.com";
                        }
                        if (description == null) {
                            description = "description";
                        }
                        if (status == null) {
                            status = "False";
                        }
                        activity.setScheduleActivity(new ScheduleActivityBO().getById(Integer.parseInt(scheduleid), true));
                        activity.setTypeOfActivity(new TypeOfActivityBO().getById(Integer.parseInt(type_activityid), true));
                        activity.setTitleActivity(title);
                        activity.setDateOfActivity(new SimpleDateFormat("dd/MM/yyyy").format(new SimpleDateFormat("dd/MM/yyyy").parse(date)));
                        activity.setTimeDuaration(timeduration);
                        activity.setLocaltion(location);
                        activity.setFee(Integer.parseInt(fee));
                        activity.setCurrentPeople(Integer.parseInt(current_people));
                        activity.setLimittedPeople(Integer.parseInt(limitted_people));
                        activity.setEmailOfOrganizer(email);
                        activity.setDescription(description);
                        activity.setStatusOfActivity(status);
                        //edit
                        boolean kq = new ActivityBO().update(activity);
                        if (kq) {
                            out.println("<result>success</result>");
                        }

                    } else {
                        out.println("<result>empty</result>");
                    }
                } else {
                    out.println("<result>empty</result>");
                }
            }
            
            if(action.equals(Constants.ACTION_UPDATE)){
                String activityid = request.getParameter(Constants.ACTIVITY_ID);
                //out.println(activityid);
                Activity activity = new ActivityBO().getById(Integer.parseInt(activityid), true);
                activity.setCurrentPeople(activity.getCurrentPeople()+1);
                boolean kq = new ActivityBO().update(activity);
                if(kq){
                    out.println("<result>success</result>");
                }
            }

            if (action.equals(Constants.ACTION_DELETE)) {
                String activityid = request.getParameter(Constants.ACTIVITY_ID);
                if (activityid != null) {
                    Activity activity = new ActivityBO().getById(Integer.parseInt(activityid), true);
                    //delete
                    boolean kq = new ActivityBO().delete(activity);
                    if (kq) {
                        out.println("<result>success</result>");
                    }
                }else{
                    out.println("<result>empty</result>");
                }
            }         

        } catch (Exception ex) {
            out.println("<result>error</result>");
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
